#ifndef BACKUPRECORDITEM_H
#define BACKUPRECORDITEM_H

#include <QWidget>
#include <QDateTime>
#include <QPainter>
#include <QEnterEvent>
#include <QDesktopServices>
#include <qfileinfo.h>
#include "web/tcpmgr.h"
#include "sql/sqlite/historydao.h"

namespace Ui {
class BackupRecordItem;
}

class BackupRecordItem : public QWidget
{
    Q_OBJECT

public:
    explicit BackupRecordItem(QWidget *parent = nullptr,
                              FileInfo info = {});
    ~BackupRecordItem();
    void setIsDownloaded(bool isDownloaded){m_isDownloaded = isDownloaded;}
    bool isDownloaded(){return m_isDownloaded;}
    bool isSelected() const { return m_isSelected; }
    void setSelected(bool selected);
    void setHovered(bool isHovered){m_isHovered = isHovered;}
    FileInfo getFileInfo() const { return m_recordFileInfo; }
protected:
    void paintEvent(QPaintEvent *event) override;
    void enterEvent(QEnterEvent *event) override;
    void leaveEvent(QEvent *event) override;
signals:
    void selectionChanged(bool selected);

    void attemptToDownload();

    void deleteThisRecord(BackupRecordItem*);
private slots:
    void on_DownloadBtn_clicked();

    void on_DeleteBtn_clicked();

    void on_checkBox_clicked();

    void updateStatus(int file_id);

private:
    Ui::BackupRecordItem *ui;
    FileInfo m_recordFileInfo;
    bool m_isSelected = false;
    bool m_isHovered = false;
    bool m_isDownloaded = false;

    QString formatFileSize(quint64 bytes) {
        constexpr quint64 KB = 1024;
        constexpr quint64 MB = KB * 1024;
        constexpr quint64 GB = MB * 1024;

        if (bytes >= GB) {
            return QString("%1 GB").arg(QString::number(bytes / (double)GB, 'f', 2));
        } else if (bytes >= MB) {
            return QString("%1 MB").arg(QString::number(bytes / (double)MB, 'f', 2));
        } else if (bytes >= KB) {
            return QString("%1 KB").arg(QString::number(bytes / (double)KB, 'f', 2));
        } else {
            return QString("%1 B").arg(bytes);
        }
    }
};

#endif // BACKUPRECORDITEM_H
